package com.jw.blog.wblog.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jw.blog.annotation.SystemLog;
import com.jw.blog.utils.ConstantUtil;
import com.jw.blog.wblog.entity.BlackIp;
import com.jw.blog.wblog.entity.custom.ResultBean;
import com.jw.blog.wblog.service.IBlackIpService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wan Email-wlf18726994755@163.com
 * @version 1.0
 * @className BlackIpControllerAdmin
 * @date 2020/6/21 15:05
 **/
@RestController
public class BlackIpControllerAdmin {
    @Resource
    private IBlackIpService iBlackIpService;

    @PostMapping("/admin/selectAllBlackIpCount")
    public ResultBean<Integer> selectAllBlackIpCount() {
        int count = iBlackIpService.count();
        ResultBean<Integer> resultBean = new ResultBean<>();
        resultBean.setData(count);
        return resultBean;
    }


    /**
     * 添加黑名单的功能
     *
     * @param prarm   。。。
     * @param blackIp 。。。。
     * @return 返回结果
     */
    @PostMapping("/admin/addBlackIp")
    @SystemLog(description = ConstantUtil.BACKIP_ADD,userType= ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<BlackIp> addBlackIp(String prarm, BlackIp blackIp) {
        ResultBean<BlackIp> resultBean = new ResultBean<>();
        QueryWrapper<BlackIp> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(BlackIp::getIp, blackIp.getIp());
        if (iBlackIpService.list(wrapper).size() != 0) {
            // 已经存在该IP
            resultBean.setCode(2);
        } else {
            blackIp.setTime(LocalDateTime.now());
            if (iBlackIpService.save(blackIp)) {
                resultBean.setCode(ResultBean.SUCCESS);
            } else {
                // 0表示：更新失败
                resultBean.setCode(ResultBean.NO_PERMISSION);
            }
        }
        return resultBean;
    }

    @PostMapping("/admin/selectLikeBlackIpListByPage")
    public ResultBean<IPage<BlackIp>> selectLikeBlackIpListByPage(
            BlackIp blackIp,
            @RequestParam(value = "startTime") String startTime,
            @RequestParam(value = "endTime") String endTime,
            @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = true, defaultValue = "9") Integer pageSize) {
        QueryWrapper<BlackIp> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasEmpty(startTime)) {
            wrapper.lambda().ge(BlackIp::getTime,startTime);
        }
        if (!StrUtil.hasEmpty(endTime)) {
            wrapper.lambda().le(BlackIp::getTime,endTime);
        }
        if (!StrUtil.hasEmpty(blackIp.getIp())) {
            wrapper.lambda().like(BlackIp::getIp,blackIp.getIp()).or();
        }
        if (!StrUtil.hasEmpty(blackIp.getCity())) {
            wrapper.lambda().like(BlackIp::getCity,blackIp.getCity()).or();
        }
        if (!StrUtil.hasEmpty(blackIp.getPlatformType())) {
            wrapper.lambda().like(BlackIp::getPlatformType,blackIp.getPlatformType()).or();
        }
        if (!StrUtil.hasEmpty(blackIp.getBrowserType())) {
            wrapper.lambda().like(BlackIp::getBrowserType,blackIp.getBrowserType());
        }
        wrapper.lambda().orderByDesc(BlackIp::getTime);
        // 分页显示：第1页开始，每页显示9条记录
        Page<BlackIp> ipPage = new Page<>(page,pageSize);
        Page<BlackIp> page1 = iBlackIpService.page(ipPage, wrapper);
        ResultBean<IPage<BlackIp>> resultBean = new ResultBean<>();
        if (page1.getRecords().size() != 0){
            resultBean.setData(page1);
        }else {
            resultBean.setCode(ResultBean.FAIL);
            resultBean.setMsg("数据为空");
        }
        return resultBean;
    }

    @PostMapping("/admin/deleteBlackIp")
    @SystemLog(description = ConstantUtil.BACKIP_DELETE,userType=ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> deleteBlackIp(String param,@RequestParam(value = "id") Integer id){
        ResultBean<Object> resultBean = new ResultBean<>();
        QueryWrapper<BlackIp> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BlackIp::getVid,id);
        if (iBlackIpService.remove(wrapper)) {
            resultBean.setCode(ResultBean.SUCCESS);
        } else {
            // 0表示：失败
            resultBean.setCode(ResultBean.NO_PERMISSION);
        }
        return resultBean;
    }
}
